//
//  JKIndexMyWelfareListVC.m
//  chequanbao_ios
//
//  Created by 朱凯 on 2018/1/24.
//  Copyright © 2018年 com.itboye. All rights reserved.
//

#import "JKIndexMyWelfareListVC.h"

// vc
#import "JKIndexMyWelfareVC.h"          // 福利
#import "JKIndexMyWelfareFriendsVC.h"   // 亲朋好友
#import "JKIndexMyWelfareRedPacketVC.h" // 红包明细
#import "ByUIMineConsumeVC.h"           // 我的消费

@interface JKIndexMyWelfareListVC ()

@end

@implementation JKIndexMyWelfareListVC

#pragma mark - 生命周期
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"我的福利";
    self.view.backgroundColor = TABLE_COLOR;
    
    [[self.view viewWithTag:888] removeFromSuperview];
    
    [self addColorLeftBackBarButtonItem];
    
    [self createViews];
    
}
- (void)viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:animated];
    
    [self setNaviAttributeWithTranslucent:false stateBarLightContent:false];
}

#pragma mark - 创建视图
- (void)createViews {
    
    UIView *bgView = [UIView new];
    bgView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:bgView];
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.equalTo(@10);
        make.right.equalTo(@-10);
        make.height.equalTo(@400);
    }];
    
    
    NSArray *titles = @[@"我的推荐码", @"我的福利", @"亲朋好友", @"红包明细", @"消费明细"];
    __block UIControl *last = nil;
    [titles enumerateObjectsUsingBlock:^(NSString *title, NSUInteger idx, BOOL * _Nonnull stop) {
        
        UIControl *basalView = [UIControl new];
        basalView.tag = idx;
        [basalView addTarget:self action:@selector(tapAction:) forControlEvents:UIControlEventTouchUpInside];
        [bgView addSubview:basalView];
        
        UIImageView *imageView = [UIImageView new];
        imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"福利-%@", title]];
        [basalView addSubview:imageView];
        
        UILabel *label = [[UILabel alloc] init];
        label.adjustsFontSizeToFitWidth = true;
        label.text = title;
        label.font = FONT(15);
        label.textAlignment = NSTextAlignmentCenter;
        label.textColor = [UIColor blackColor];
        [basalView addSubview:label];
        
        
        [basalView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.equalTo(bgView).multipliedBy(1 / 3.0);
            if (last) {
                // 第一行
                if (idx / 3 == 0) {
                    make.top.equalTo(last);
                    make.left.equalTo(last.mas_right);
                } else {
                    if (idx % 3 == 0) {
                        make.top.equalTo(last.mas_bottom).offset(20);
                        make.left.equalTo(@0);
                    } else {
                        make.top.equalTo(last);
                        make.left.equalTo(last.mas_right);
                    }
                }
            } else {
                make.top.equalTo(@20);
                make.left.equalTo(@0);
            }
            
           
        }];
        [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.height.equalTo(bgView.mas_width).multipliedBy(0.2);
            make.top.equalTo(@10);
            make.centerX.equalTo(@0);
        }];
        [label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(@0);
            make.top.equalTo(imageView.mas_bottom).offset(10);
            make.height.equalTo(@20);
            // 设置basalView的高度
            make.bottom.equalTo(@-10);
        }];
        
        last = basalView;
    }];
}

#pragma mark - 事件
- (void)tapAction:(UIControl *)sender {
    if (!BY_USER) {
        ByUILoginVC *vc = [[ByUILoginVC alloc] init];
        ByBaseNaviController *navi = [[ByBaseNaviController alloc] initWithRootViewController:vc];
        [self presentViewController:navi animated:YES completion:nil];
        return;
    }
    
    switch (sender.tag) {
        case 0: // 我的推荐码
        {
            [self dismissViewControllerAnimated:true completion:nil];
            ByTabBarController *vc = (ByTabBarController *)BY_APPDELEGATE.window.rootViewController;
            vc.selectedIndex = 1;
        }
            break;
        case 1: // 我的福利
        {
            JKIndexMyWelfareVC *vc = [JKIndexMyWelfareVC new];
            [self by_normalPush:vc hasNavi:true];
        }
            break;
        case 2: // 亲朋好友
        {
            JKIndexMyWelfareFriendsVC *vc = [JKIndexMyWelfareFriendsVC new];
            [self by_normalPush:vc hasNavi:true];
        }
            break;
        case 3: // 红包明细
        {
            JKIndexMyWelfareRedPacketVC *vc = [JKIndexMyWelfareRedPacketVC new];
            [self by_normalPush:vc hasNavi:true];
        }
            break;
        case 4: // 消费明细
        {
            ByUIMineConsumeVC *vc = [ByUIMineConsumeVC new];
            [self by_normalPush:vc hasNavi:true];
        }
            break;
            
            
        default:
            break;
    }
}



@end
